<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\Common\model;

class LongbingUser extends \app\BaseModel
{
    protected $name = "longbing_card_user";
    public function getUser($filter)
    {
        $filter["deleted"] = 0;
        $result = $this->where($filter)->find();
        if(!empty($result)) {
            $result = $result->toArray();
        }
        return $result;
    }
    public function listCustomerData($filter, $page_config)
    {
        $uniacid = $filter["uniacid"];
        $where = [["user.uniacid", "=", $uniacid], ["user.is_staff", "=", 0]];
        if(isset($filter["is_phone"]) && !empty($filter["is_phone"])) {
            $where[] = ["phone.phone", "<>", "null"];
        }
        if(isset($filter["avatarUrl"]) && !empty($filter["avatarUrl"])) {
            $where[] = ["user.avatarUrl", "<>", ""];
        }
        $mark_arr = [["id" => 3, "value" => "未跟进"], ["id" => 1, "value" => "跟进中"], ["id" => 2, "value" => "已成交"]];
        $deal_arr = [["id" => 1, "value" => "未成交"], ["id" => 2, "value" => "已成交"]];
        $count = 0;
        $users = [];
        $mark_value = "";
        $deal_value = "";
        if(isset($filter["mark"]) || isset($filter["deal"]) || isset($filter["nickName"])) {
            $user_ids = [];
            if(isset($filter["mark"])) {
                $mark_value = $filter["mark"];
                if($filter["mark"] == 3) {
                    $mark_model = new LongbingCardUserMark();
                    $list_mark = $mark_model->listMarkData(["uniacid" => $uniacid, "status" => 1], ["user_id"]);
                    $tmp_arr = [];
                    foreach ($list_mark as $k => $v) {
                        array_push($tmp_arr, $v["user_id"]);
                    }
                    if(1 < count($tmp_arr)) {
                        $tmp_arr = "(" . implode(",", $tmp_arr) . ")";
                        $list_mark = $this->where([["uniacid", "=", $uniacid], ["id", "not in", $tmp_arr], ["is_staff", "=", 0]])->field(["id"])->select();
                    } elseif(count($tmp_arr) == 1) {
                        $tmp_arr = implode(",", $tmp_arr);
                        $list_mark = $this->where([["uniacid", "=", $uniacid], ["id", "!=", $tmp_arr], ["is_staff", "=", 0]])->field(["id"])->select();
                    } else {
                        $list_mark = $this->where([["uniacid", "=", $uniacid], ["is_staff", "=", 0]])->field(["id"])->select();
                    }
                    if(empty($list_mark)) {
                        $list_mark = [];
                    }
                    foreach ($list_mark as $k => $v) {
                        array_push($user_ids, $v["id"]);
                    }
                } else {
                    $mark_model = new LongbingCardUserMark();
                    $list_mark = $mark_model->listMarkData(["uniacid" => $uniacid, "status" => 1], ["user_id"]);
                    foreach ($list_mark as $k => $v) {
                        array_push($user_ids, $v["user_id"]);
                    }
                }
            }
            if(isset($filter["deal"])) {
                $deal_value = $filter["deal"];
                if($filter["deal"] == 1) {
                    $mark_model = new LongbingCardUserMark();
                    $list_mark = $mark_model->listMarkData(["uniacid" => $uniacid, "mark" => 2, "status" => 1], ["user_id"]);
                    if(empty($list_mark)) {
                        $list_mark = [];
                    }
                    if(isset($filter["mark"])) {
                        $tmp1 = [];
                        foreach ($list_mark as $v) {
                            if(!in_array($v["user_id"], $tmp1)) {
                                $tmp1[] = $v["user_id"];
                            }
                        }
                        $tmp2 = [];
                        foreach ($user_ids as $v) {
                            if(!in_array($v, $tmp1)) {
                                $tmp2[] = $v;
                            }
                        }
                        $user_ids = $tmp2;
                    } else {
                        $tmp_arr = [];
                        foreach ($list_mark as $k => $v) {
                            array_push($tmp_arr, $v["user_id"]);
                        }
                        if(1 < count($tmp_arr)) {
                            $tmp_arr = "(" . implode(",", $tmp_arr) . ")";
                            $list_mark = $this->where([["uniacid", "=", $uniacid], ["id", "not in", $tmp_arr], ["is_staff", "=", 0]])->field(["id"])->select();
                        } elseif(count($tmp_arr) == 1) {
                            $tmp_arr = implode(",", $tmp_arr);
                            $list_mark = $this->where([["uniacid", "=", $uniacid], ["id", "!=", $tmp_arr], ["is_staff", "=", 0]])->field(["id"])->select();
                        } else {
                            $list_mark = $this->where([["uniacid", "=", $uniacid], ["is_staff", "=", 0]])->field(["id"])->select();
                        }
                        if(empty($list_mark)) {
                            $list_mark = [];
                        }
                        foreach ($list_mark as $k => $v) {
                            array_push($user_ids, $v["id"]);
                        }
                    }
                } else {
                    $mark_model = new LongbingCardUserMark();
                    $list_mark = $mark_model->listMarkData(["uniacid" => $uniacid, "mark" => 2, "status" => 1], ["user_id"]);
                    if(isset($filter["mark"])) {
                        $tmp1 = [];
                        $tmp2 = [];
                        foreach ($list_mark as $k => $v) {
                            if(!in_array($v["user_id"], $tmp1)) {
                                $tmp1[] = $v["user_id"];
                            }
                        }
                        foreach ($tmp1 as $v) {
                            if(in_array($v, $user_ids) && !in_array($v, $tmp2)) {
                                $tmp2[] = $v;
                            }
                        }
                        $user_ids = $tmp2;
                    } else {
                        foreach ($list_mark as $k => $v) {
                            array_push($user_ids, $v["user_id"]);
                        }
                    }
                }
            }
            if(isset($filter["nickName"])) {
                $search = "%" . $filter["nickName"] . "%";
                $client_model = new LongbingClientInfo();
                $users1 = $client_model->listClientData([["uniacid", "=", $uniacid], ["name", "like", $search]], ["user_id"]);
                $users2 = $this->where([["uniacid", "=", $uniacid], ["nickName", "like", $search]])->field(["id"])->select();
                $tmp = [];
                foreach ($users1 as $k => $v) {
                    if(!in_array($v["user_id"], $tmp)) {
                        array_push($tmp, $v["user_id"]);
                    }
                }
                foreach ($users2 as $k => $v) {
                    if(!in_array($v["id"], $tmp)) {
                        array_push($tmp, $v["id"]);
                    }
                }
                $tmp = array_unique($tmp);
                if(isset($filter["mark"]) || isset($filter["deal"])) {
                    $tmp1 = [];
                    $tmp2 = $user_ids;
                    foreach ($tmp as $k => $v) {
                        if(in_array($v, $tmp2) && !in_array($v, $tmp1)) {
                            array_push($tmp1, $v);
                        }
                    }
                    $user_ids = $tmp1;
                } else {
                    foreach ($tmp as $k => $v) {
                        if(!in_array($v, $user_ids)) {
                            array_push($user_ids, $v);
                        }
                    }
                }
            }
            $where[] = ["user.id", "in", $user_ids];
            $result = $this->alias("user")->leftJoin("longbing_card_user_phone phone", "user.id = phone.user_id")->where($where);
            $count = $result->count();
            $users = $result->page($page_config["page"], $page_config["page_count"])->field("user.* ,phone.phone")->order("user.id desc")->select()->toArray();
        } else {
            $result = $this->alias("user")->leftJoin("longbing_card_user_phone phone", "user.id = phone.user_id")->where($where);
            $count = $result->count();
            $users = $result->page($page_config["page"], $page_config["page_count"])->field("user.* ,phone.phone")->order("user.id desc")->select()->toArray();
        }
        foreach ($users as $k => $v) {
            $users[$k]["user_name"] = "";
            $client_model = new LongbingClientInfo();
            $client_info = $client_model->getClientData([["uniacid", "=", $uniacid], ["user_id", "=", $v["id"]]], ["name"]);
            if($client_info) {
                $users[$k]["user_name"] = $client_info["name"];
            }
            $users[$k]["deal_time"] = "";
            $collection_model = new LongbingCardCollection();
            $collections = $collection_model->listCollection(["uniacid" => $uniacid, "uid" => $v["id"], "status" => 1]);
            $users[$k]["mark"] = 0;
            if(isset($filter["mark"]) && in_array($filter["mark"], [1, 2, "1", "2"])) {
                $users[$k]["mark"] = $filter["mark"];
            }
            if(isset($filter["deal"]) && in_array($filter["deal"], [2, "2"])) {
                $users[$k]["mark"] = 2;
            }
            $mark_staff = "";
            $mark_model = new LongbingCardUserMark();
            $list_mark = $mark_model->where(["uniacid" => $uniacid, "status" => 1, "user_id" => $v["id"], "staff_id" => $v["last_staff_id"]])->value("mark");
            $users[$k]["mark"] = !empty($list_mark) ? $list_mark : 0;
            foreach ($collections as $collection) {
                $staff = longbingGetUserInfo($collection["to_uid"], $uniacid);
                if(!empty($staff) && isset($staff["is_staff"]) && !empty($staff["is_staff"])) {
                    if(!empty($mark_staff)) {
                        $mark_staff = $mark_staff . ",";
                    }
                    $mark_staff = $mark_staff . $staff["name"];
                    $mark_model = new LongbingCardUserMark();
                    $mark = $mark_model->getMarkData(["user_id" => $v["id"], "staff_id" => $collection["to_uid"], "status" => 1], ["id", "mark"]);
                    if(!empty($mark)) {
                        if(in_array($mark["mark"], [2, "2"])) {
                            $mark_staff = $mark_staff . "(" . lang("traded") . ")";
                        } else {
                            $mark_staff = $mark_staff . "(" . lang("not traded") . ")";
                        }
                        if($users[$k]["mark"] < $mark["mark"]) {
                            $users[$k]["mark"] = $mark["mark"];
                        }
                    } else {
                        $mark_staff = $mark_staff . "(" . lang("not traded") . ")";
                    }
                }
            }
            $users[$k]["mark_staff"] = $mark_staff;
            $users[$k]["rate"] = 0;
            if($v["import"] == 1) {
            }
        }
        $result = ["data" => $users, "count" => $count];
        return $result;
    }
    public function listCustomerDataV2($filter)
    {
        $mark_model = new LongbingCardUserMark();
        $client_model = new LongbingClientInfo();
        $collection_model = new LongbingCardCollection();
        $uniacid = $filter["uniacid"];
        $where = [["user.uniacid", "=", $uniacid], ["user.is_staff", "=", 0]];
        if(!empty($filter["mark"])) {
            $dis = ["uniacid" => $uniacid, "status" => 1];
            if($filter["mark"] != 3) {
                $dis["mark"] = $filter["mark"];
            }
            $user_ids = $mark_model->where($dis)->column("user_id");
            if($filter["mark"] == 3) {
                $user_ids = $this->where([["uniacid", "=", $uniacid], ["id", "not in", $user_ids], ["is_staff", "=", 0]])->field(["id"])->column("id");
            }
            $where[] = ["user.id", "in", $user_ids];
        }
        if(isset($filter["nickName"])) {
            $where[] = ["user.nickName", "like", "%" . $filter["nickName"] . "%"];
        }
        if(!empty($filter["empower"]) && $filter["empower"] == 1) {
            $where[] = ["phone.phone", "<>", "null"];
        }
        if(!empty($filter["empower"]) && $filter["empower"] == 2) {
            $where[] = ["user.avatarUrl", "<>", ""];
        }
        if(!empty($filter["start_time"]) && !empty($filter["end_time"])) {
            $start_time = $filter["start_time"];
            $end_time = $filter["end_time"];
            $where[] = ["user.create_time", "between", $start_time . "," . $end_time];
        }
        $usersdata = $this->alias("user")->leftJoin("longbing_card_user_phone phone", "user.id = phone.user_id")->where($where)->field("user.* ,phone.phone")->order("user.id desc")->paginate($filter["limit"])->toArray();
        $users = $usersdata["data"];
        if(!empty($users)) {
            foreach ($users as $k => $v) {
                $users[$k]["user_name"] = $client_model->where([["uniacid", "=", $uniacid], ["user_id", "=", $v["id"]]], ["name"])->value("name");
                $users[$k]["mark"] = !empty($filter["mark"]) ? $filter["mark"] : 0;
                $users[$k]["deal_time"] = "";
                $mark_staff = "";
                $list_mark = $mark_model->where(["uniacid" => $uniacid, "status" => 1, "user_id" => $v["id"], "staff_id" => $v["last_staff_id"]])->value("mark");
                $users[$k]["mark"] = !empty($list_mark) ? $list_mark : 0;
                $users[$k]["update_time"] = $mark_model->where(["uniacid" => $uniacid, "status" => 1, "user_id" => $v["id"], "mark" => 2])->value("update_time");
                $users[$k]["update_time"] = !empty($users[$k]["update_time"]) ? $users[$k]["update_time"] : "-";
                $collections = $collection_model->listCollection(["uniacid" => $uniacid, "uid" => $v["id"], "status" => 1]);
                $rate_text = "";
                $remark_name = "";
                foreach ($collections as $collection) {
                    $staff = longbingGetUserInfo($collection["to_uid"], $uniacid);
                    if(!empty($staff) && isset($staff["is_staff"]) && !empty($staff["is_staff"])) {
                        if(!empty($mark_staff)) {
                            $mark_staff = $mark_staff . ",";
                        }
                        if(empty($staff["name"])) {
                            $staff["name"] = $this->where(["id" => $collection["to_uid"]])->value("nickName");
                        }
                        $mark_staff = $mark_staff . $staff["name"];
                        $mark = $mark_model->getMarkData(["user_id" => $v["id"], "staff_id" => $collection["to_uid"], "status" => 1], ["id", "mark"]);
                        if(!empty($mark)) {
                            if(in_array($mark["mark"], [2, "2"])) {
                                $mark_staff = $mark_staff . "(" . lang("traded") . ")";
                            } else {
                                $mark_staff = $mark_staff . "(" . lang("In the follow up") . ")";
                            }
                            if($users[$k]["mark"] < $mark["mark"]) {
                                $users[$k]["mark"] = $mark["mark"];
                            }
                        } else {
                            $mark_staff = $mark_staff . "(" . lang("not follow up") . ")";
                        }
                        $rate_text .= $staff["name"] . "(" . $collection["rate"] . "%)";
                    }
                    $dis = ["user_id" => $v["id"], "staff_id" => $collection["to_uid"]];
                    $staff_name = \think\facade\Db::name("longbing_card_client_info")->where($dis)->value("name");
                    if(!empty($staff_name) || !empty($staff["name"])) {
                        $remark_name .= $staff["name"] . "(" . $staff_name . ")";
                    }
                }
                $users[$k]["mark_staff"] = $mark_staff;
                $users[$k]["rate"] = 0;
                $users[$k]["rate_text"] = $rate_text;
                $users[$k]["remark_name"] = $remark_name;
            }
        }
        $usersdata["data"] = $users;
        return $usersdata;
    }
    public function listCustomer($filter, $page_config)
    {
    }
    public function getCoustomerCount($filter)
    {
        $filter_data["user.is_staff"] = 0;
        $filter_data["user.deleted"] = 0;
        if(isset($filter["uniacid"])) {
            $filter_data["user.uniacid"] = $filter["uniacid"];
        }
        $result = $this->alias("user")->where($filter_data);
        if(isset($filter["nickName"])) {
            $result = $result->where("user.nickName", "like", "%" . $filter["nickName"] . "%");
        }
        $result = $result->leftJoin("longbing_card_user_mark mark", "mark.user_id = user.id");
        if(isset($filter["mark"])) {
            switch ($filter["mark"]) {
                case 0:
                    $result = $result->whereOr([["mark.mark", "in", [0]], ["mark.mark", "=", NULL]]);
                    break;
                case 1:
                    $result = $result->where("mark.mark", "=", 1);
                    break;
                case 2:
                    $result = $result->where("mark.mark", "=", 2);
                    break;
                case 3:
                    $result = $result->whereOr([["mark.mark", "in", [0, 1]], ["mark.mark", "=", NULL]]);
                    break;
            }
        }
        $result = $result->leftjoin("longbing_card_user_phone phone", "user.id = phone.user_id");
        if(isset($filter["is_phone"]) && !empty($filter["is_phone"])) {
            $result->where("phone.phone", "<>", NULL);
        }
        $count = $result->count();
        return $count;
    }
    public function getCustomer($filter)
    {
        $filter_data["user.is_staff"] = 0;
        $filter_data["user.deleted"] = 0;
        if(isset($filter["uniacid"])) {
            $filter_data["user.uniacid"] = $filter["uniacid"];
        }
        if(isset($filter["customer_id"])) {
            $filter_data["user.id"] = $filter["customer_id"];
        }
        $result = $this->alias("user")->where($filter_data);
        $result = $result->leftjoin("longbing_card_user_phone phone", "user.id = phone.user_id");
        $result = $result->field("user.* ,phone.phone")->find();
        if(!empty($result)) {
            $result = $result->toArray();
        }
        return $result;
    }
    public function updateCustomer($filter, $data)
    {
        $data["update_time"] = time();
        $result = $this->where($filter)->update($data);
        return !empty($result);
    }
    public function updateUser($filter, $data)
    {
        $data["update_time"] = time();
        $result = $this->where($filter)->update($data);
        return !empty($result);
    }
    public function getUserCount($filter = [])
    {
        $count = $this->where($filter)->count();
        return $count;
    }
}

?>